package com.cskaoyan.aspect;

import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.LogMapper;
import com.cskaoyan.mapper.RoleMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.model.bean.Admin;
import com.cskaoyan.model.bean.Log;
import com.cskaoyan.model.bean.LogExample;
import com.cskaoyan.model.bean.User;
import com.cskaoyan.model.bo.systembo.CreateAdminBo;
import com.cskaoyan.model.bo.systembo.DeleteAdminBo;
import com.cskaoyan.model.bo.systembo.DeleteRoleBo;
import com.cskaoyan.model.bo.systembo.UpdateAdminBo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class LogAspect {

    @Autowired
    LogMapper logMapper;

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    RoleMapper roleMapper;

    @Pointcut("@annotation(com.cskaoyan.aspect.LogA)")
    public void mypointcut() {
    }

    @Around("mypointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        String name = joinPoint.getSignature().getName();
        String adminNamePre = (String) SecurityUtils.getSubject().getPrincipal();
        Admin adminUpdate = new Admin();
        if (adminNamePre != null && !"".equals(adminNamePre)) {
            if (name.contains("update")) {
                Object[] args = joinPoint.getArgs();
                UpdateAdminBo updateAdminBoPre = (UpdateAdminBo) args[0];
                adminUpdate = adminMapper.selectByPrimaryKey(updateAdminBoPre.getId());
            }
        }

        Object proceed = joinPoint.proceed();

        String adminName = (String) SecurityUtils.getSubject().getPrincipal();
        String host = SecurityUtils.getSubject().getSession().getHost();
        Log log = new Log();
        if (adminName == null || "".equals(adminName)) {
            log.setAdmin("匿名用户");
        }
        log.setAdmin(adminName);
        log.setIp(host);
        log.setType(1);

        if (name.contains("login")) {
            log.setAction("登录");
        }
        if (name.contains("logout")) {
            log.setAction("登出");
            log.setAdmin(adminNamePre);
        }
        if (name.contains("create")) {
            log.setAction("创建管理员");
            Object[] args = joinPoint.getArgs();
            CreateAdminBo createAdminBo = (CreateAdminBo) args[0];
            log.setResult(createAdminBo.getUsername());
            log.setComment("尊贵的超级管理员Admin123创建了管理员" + createAdminBo.getUsername());
        }
        if (name.contains("delete")) {
            log.setAction("删除管理员");
            Object[] args = joinPoint.getArgs();
            DeleteAdminBo deleteAdminBo = (DeleteAdminBo) args[0];
            log.setResult(deleteAdminBo.getUsername());
            log.setComment("尊贵的超级管理员Admin123将" + deleteAdminBo.getUsername() + "踢出了群聊");

        }
        if (name.contains("update")) {
            log.setAction("编辑管理员");
            Object[] args = joinPoint.getArgs();
            UpdateAdminBo updateAdminBo = (UpdateAdminBo) args[0];
            StringBuilder stringBuilder = new StringBuilder();
            if (!adminUpdate.getUsername().equals(updateAdminBo.getUsername())) {
                stringBuilder.append("修改管理员的名字为" + updateAdminBo.getUsername() + " ");
            }
            if (!Arrays.equals(adminUpdate.getRoleIds(), updateAdminBo.getRoleIds())) {
                stringBuilder.append("将管理员的身份调整为");
                Integer[] roleIds = updateAdminBo.getRoleIds();
                for (Integer roleId : roleIds) {
                    String roleName = roleMapper.selectNameByRoleId(roleId);
                    stringBuilder.append(roleName + " ");
                }
            }
            log.setComment(stringBuilder.toString());
        }
        log.setStatus(true);
        log.setDeleted(false);
        log.setAddTime("now");
        log.setUpdateTime("now");
        logMapper.insertSelective(log);


        return proceed;
    }
}
